package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private SearchFeign searchFeign;

    /**
     * 测试远程调用, 这里是调用方
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        //1. 通过Eureka客户端获取被调用方Search服务的信息
        //InstanceInfo instance = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);
        //2. 获取到访问地址, url
        //String url = instance.getHomePageUrl();
        //System.out.println("====url====" + url);
        //3. 通过restTemplate发送http协议请求, 远程调用
//        String str = restTemplate.getForObject(url + "/search/test", String.class);

        //远程调用, 并且加入ribbon接口负载均衡技术
        String str = restTemplate.getForObject("http://QF-SEARCH/search/test", String.class);
        //4. 返回结果
        return str;
    }

    /**
     * 演示feign远程调用
     * @return
     */
    @GetMapping("/hello2")
    public String hello2() {
        //远程调用
        String str = searchFeign.test();
        return str;
    }
}
